package com.css.fxfzaqbz.modules.data.controller;

import com.css.fxfzaqbz.modules.data.entity.GeologicalDisasters;
import com.css.fxfzaqbz.modules.data.queryParams.GeologicalDisastersQueryParams;
import com.css.fxfzaqbz.modules.data.service.GeologicalDisastersService;
import com.css.fxfzaqbz.base.response.RestResponse;

import com.css.fxfzaqbz.util.FileUtil2;
import com.css.fxfzaqbz.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 *
 *
 * 3.3.3次生灾害数据管理子模块 地质灾害数据
 */
@RestController
@RequestMapping("/fxfzaqbz/data")
@Slf4j
public class GeologicalDisastersController {

    @Resource
    private GeologicalDisastersService geologicalDisastersService;

    /**
     * 新增
     * @param geologicalDisasters
     * @return
     */
    @PostMapping("/saveGeologicalDisasters")
    public RestResponse saveGeological(@RequestBody GeologicalDisasters geologicalDisasters){
        try {
            GeologicalDisasters geologicalDisasters1 = geologicalDisastersService.saveGeologicalDisasters(geologicalDisasters);

            return RestResponse.succeed(geologicalDisasters1);
        } catch (Exception e) {
            log.error("新增地质灾害数据失败",e);
            return RestResponse.fail("新增地质灾害数据失败");
        }
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/getGeologicalDisastersById/{id}")
    public RestResponse getGeologicalDisastersById(@PathVariable("id")String id){
        RestResponse restResponse = null;
        try{
            restResponse = RestResponse.succeed(geologicalDisastersService.getGeologicalDisastersById(id));
        }catch (Exception e){
            log.error("获取地质灾害数据失败!",e);
            restResponse = RestResponse.fail("获取地质灾害数据失败!");
        }
        return restResponse;
    }


   /* @PostMapping("/updateGeological")
    public RestResponse updateGeological(@RequestBody HouseDTO houseDTO){
        try {
            HouseDTO houseDTO1= houseService.updateHouse(houseDTO);
            return RestResponse.succeed(houseDTO1);
        } catch (Exception e) {
            log.error("房屋造价数据修改失败",e);
            return RestResponse.fail("房屋造价数据修改失败");
        }
    }*/

    @PostMapping("/deleteGeologicalDisasters/{ids}")
    public RestResponse deleteGeological(@PathVariable("ids") List<String> ids){
        try {
            geologicalDisastersService.deleteGeologicalDisasters(ids);
            return RestResponse.succeed("删除地质灾害数据成功");
        } catch (Exception e) {
            log.error("删除地质灾害数据失败",e);
            return RestResponse.fail("删除地质灾害数据失败");
        }
    }

    @GetMapping("/getAllGeologicalDisasters")
    public RestResponse getAllGeological(HttpServletRequest request, GeologicalDisastersQueryParams queryParams){
        RestResponse restResponse = null;
        try{
            //  int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            //   int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            queryParams.setCurPage(Integer.parseInt(PlatformPageUtils.CUR_PAGE_DEF));
            queryParams.setPageSize(Integer.parseInt(PlatformPageUtils.PAGE_SIZE_DEF));
            restResponse = RestResponse.succeed(geologicalDisastersService.getAllGeologicalDisasters(queryParams));
        }catch (Exception e){
            log.error("查询地质灾害数据失败!",e);
            restResponse = RestResponse.fail("查询地质灾害数据失败");
        }
        return restResponse;
    }




    /**
     * 导出表内数据
     * @param response
     * @throws IOException
     */
    @PostMapping("/exportExcelGeologicalDisasters")
    public void exportExcel(HttpServletResponse response ) throws IOException {
        RestResponse restResponse = null ;
        try{
            List<GeologicalDisasters> list  = geologicalDisastersService.getAll();
            FileUtil2.exportExcel(list, "地质灾害数据", "sheet", GeologicalDisasters.class, "GeologicalDisasters", response);
            restResponse = RestResponse.succeed("导出成功");
        }catch (Exception e){
            log.error("导出地质灾害数据失败!",e);
            restResponse = RestResponse.fail("导出地质灾害数据失败");
        }

    }

    /**
     * 导入数据
     * @param file
     * @throws IOException
     */
    @PostMapping("/importExcelGeologicalDisasters")
    public RestResponse importExcel(MultipartFile file) throws IOException {
        RestResponse restResponse = null;
        try {
            geologicalDisastersService.importExcel(file);
        } catch (Exception e) {
            log.error("导入地质灾害数据失败", e);
            restResponse = RestResponse.fail("导入地质灾害数据失败!");
        }
        return restResponse;
    }


}
